public class Test {

    //长度最小的子数组
    public static void main(String[] args) {
        int[] nums = {2,3,1,2,4,3};
        int target = 7;
        System.out.println(minSubArrayLen(target, nums));
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int sum = 0;
        //因为接下来参与比较需要得出最小值，所以为了不影响比较，我们定义为最大值
        int len = Integer.MAX_VALUE;
        for (int left = 0,right = 0; right < n; right++) {
            //进窗口
            sum += nums[right];
            while(sum >= target){//判断
                //更新结果
                len = Math.min(len,right - left + 1);
                //出窗口
                sum -= nums[left++];
            }
        }
        return len == Integer.MAX_VALUE ? 0 :len;
    }

}
